package middleware

import (
	"common/helper"
	myredis "common/redis"
	myUserHelp "common/userHelp"
	"errors"
	"github.com/valyala/fasthttp"
	"member/contrib/session"
	"member/model"
)

// 不需要token的接口加到这个列表

/*
var allows = map[string]bool{
	"/member/app/upgrade":     true,
	"/member/dataSetting/get": true,
	"/member/cs/list":         true,
	"/member/game/hot/list":   true,
	"/member/game/fav/list":   true,
	"/member/game/rec/list":   true,
	"/member/tag/list":        true,

	"/member/game/search":                   true,
	"/member/report/version":                true,
	"/member/report/pprof/":                 true,
	"/member/report/pprof/block":            true,
	"/member/report/pprof/allocs":           true,
	"/member/report/pprof/cmdline":          true,
	"/member/report/pprof/goroutine":        true,
	"/member/report/pprof/heap":             true,
	"/member/report/pprof/profile":          true,
	"/member/report/pprof/trace":            true,
	"/member/report/pprof/threadcreate":     true,
	"/member/reg":                           true,
	"/member/login":                         true,
	"/member/password/checkAccount":         true,
	"/member/password/checkPhoneAndSendSms": true,
	"/sms/send":                             true,
	"/sms/send/mail":                        true,
	"/member/lastwin":                       true,
	"/member/banner":                        true,
	"/member/notice":                        true,
	"/pay/callback":                         true,
	"/pay/jeepay/callback":                  true,
	"/pay/cepay/callback":                   true,
	"/pay/callback/epayu":                   true,
	"/pay/callback/ew":                      true,
	"/pay/callback/ewu":                     true,
	"/member/sign/config":                   true,
	"/member/treasure/config":               true,
	"/member/nav":                           true,
	"/member/game/list":                     true,
	"/member/password/forget":               true,
	"/treasure/config":                      true,
	"/sign/config":                          true,
	"/weekly/config":                        true,
	"/platform/cfg":                         true,
	"/Config/Announcement":                  true,
	"/member/game/transaction_callback":              true,
	"/member/game/number":                   true,
	"/Config/platformLink":                  true,
	"/Config/broadcast":                     true,
	"/Config/ActiveSwitchCfg":               true,
	"/Config/suspension/images":             true,
	"/Config/active/switch/list":            true,
	"/member/invite/detail":                 true,
	"/member/promo/deposit/config":          true,
	"/member/total/deposit":                 true,
	"/member/platform/list":                 true,
	"/pay/betcat/callback":                  true,
	//	"/pay/deposit":                          true,
}
*/

var allows = map[string]bool{
	"/customer-service/links":            true,
	"/games/hot":                         true,
	"/member/game/fav/list":              true,
	"/games/recommended-favorites":       true,
	"/games/recommended":                 true,
	"/games/tags":                        true,
	"/games/search":                      true,
	"/users/register":                    true,
	"/auth/login":                        true,
	"/password/check-account":            true,
	"/password/check-phone-send-sms":     true,
	"/auth/sms/send-online-phone-code":   true,
	"/auth/sms/send-mail-code":           true,
	"/system/banners":                    true,
	"/system/announcements":              true,
	"/pay/betcat/callback":               true,
	"/pay/pay4/callback":                 true,
	"/pay/u2c/callback":                  true,
	"/promotions/sign-in/config":         true,
	"/promotions/treasure-box/config":    true,
	"/games/navigation":                  true,
	"/games/list":                        true,
	"/password/reset":                    true,
	"/system/popup":                      true,
	"/member/game/transaction_callback":  true,
	"/platforms/count":                   true,
	"/config/platform-links":             true,
	"/system/broadcasts":                 true,
	"/config/active-switches":            true,
	"/promotions/deposit-bonus/config":   true,
	"/promotions/total-deposit-activity": true,
	"/platforms/list":                    true,
	"/h5/lottery/info":                   true,
	"/h5/lottery/rec":                    true,
	"/h5/lottery/rule":                   true,
	//"/account/deposits": true, //add test
}

func CheckTokenMiddleware(ctx *fasthttp.RequestCtx) (interface{}, error) {
	//有没有token都要拿到的接口要同时加到上面的allows和这个map里
	/*
		var special = map[string]bool{
			"/member/game/list":     true,
			"/member/game/hot/list": true,
			"/member/game/rec/list": true,
			"/member/game/fav/list": true,
			"/member/invite/detail": true,
			"/member/total/deposit": true,
		}

	*/
	var special = map[string]bool{
		"/games/list":                        true,
		"/games/hot":                         true,
		"/games/recommended":                 true,
		"/games/recommended-favorites":       true,
		"/promotions/total-deposit-activity": true,
		"/h5/lottery/rec":                    true,
	}

	path := string(ctx.Path())

	if _, ok := special[path]; ok {
		data, err := session.Get(ctx)
		if err == nil {
			ctx.SetUserValue("token", data)
		}
	}

	if _, ok := allows[path]; ok {
		return "", nil
	}

	resp := helper.Response{
		Status: false,
		Data:   "token",
	}
	data, err := session.Get(ctx)
	if err != nil {
		return resp, errors.New(helper.AccessTokenExpires)
	}

	ctx.SetUserValue("token", data)
	valUid := model.GetUidFromToken(ctx)

	//redis中存在说明用户曾经登陆过 判断用户是否被后台禁用
	if myredis.IsUserExis(string(valUid)) {
		if myUserHelp.GetMemberState(valUid) == 2 {
			resp = helper.Response{
				Status: false,
				Data:   "Blocked",
			}

			return resp, errors.New(helper.Blocked)
		}
	}
	return "", nil
}
